// pages/ScenicArea/ScenicAreaCommitPage/index.js
var common = require("../../../../public.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBuyerPanel: false,
    storeInfo: {},
    ticketOrder: '',
    orderNo: '',
    isLoading: false,
    orderobj:"",
    ticketTime:'',
    time: 0 ,
    timeData: {},
    flag_fail:false
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  onLoad: function (options) {
    const that = this
    console.log(options)
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
          orderNo: options.orderNo,
          orderobj:options.obj
        }, () => {
          that.initData();
        })
      }
    })
  },
  initData() {
    const that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderNo: that.data.orderNo,  
    }
    this.setData({
      isLoading: true
    })
    common.post('/venueOrder/getMyOrderInfo', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          storeInfo: res.data.store,
          ticketOrder: res.data.orderInfo,
        })
        var pastDate = Number(res.data.orderInfo.createTime);
        let  TimeTisk = setInterval(function () {
           that.PastTime(TimeTisk)
           return 
         },1000)
        that.setData({
        
          pastDate,
        },()=>{
          that.data.ticketOrder.status ==1 ? that.setData({flag_fail:false})  : that.setData({flag_fail:true}) 
        })
       
       
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoading: false
      })
    })
  },
  // 取消支付
  click_qv(){
    var that = this
    that.cancelOrder() 
  },
  // 立即支付
  click_pay(){
    this.toConfirm()
  },
    // 跳转到确定支付页面
    toConfirm() { 
            var that = this
            var tempJson = {
                  orderId:this.data.ticketOrder.id,
                  token:this.data.token,
                  openid:this.data.openid,
                  userId:that.data.userInfo.id
                };
              common.wxPayRequest("/wxxcxPayForVenue",tempJson,function(res){
                  // 测试接口暂时不能吊起
                  if(res.msg == 'OK'){
                      wx.requestPayment({
                          'timeStamp': res.data.timeStamp,
                          'nonceStr': res.data.nonceStr,
                          'package': res.data.package,
                          'signType': res.data.signType,
                          'paySign': res.data.paySign,
                          'success': function (res) {
                            common.showToast('支付成功', 'success')
                            that.initData()
                          },
                          'fail': function (res) {
                            if (res.errMsg === 'requestPayment:fail cancel') {
                            } else {
                              common.showToast(res.errMsg)
                            }
                          }
                        })
                  }
              })
     
    },
  cancelOrder() { // 取消订单
    const that = this
    common.showAlertView('提示', '是否取消该订单', function (res) {
      if (res) {
        let orderId = that.data.orderNo
        let tempJson = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderNo: orderId
        }
        common.post('/venueOrder/cancleTicketOrder', tempJson, function (res) {
          if (res.status === 200) {
            common.showToast('已取消订单')
            that.initData()
          } else {
            common.showRequestErrMsg(res)
          }
        })
      }
    })
  },
  // 计算过期时间
  PastTime(TimeTisk){
    var that = this
    let oldStamp = this.data.pastDate; //订单生成时间戳
    let newStamp = oldStamp + 30 * 60 * 1000; //订单过期时间戳
    let nowTime = new Date();
    let nowStamp = nowTime.getTime(); //现在的时间戳
    if (newStamp - nowStamp < 0) {
        // 交易失败
        this.setData({
          flag_fail:true
        })
        // that.initData()
        clearInterval(TimeTisk); //小于0,清除定时器
    }else{
      // let sec = parseInt((newStamp - nowStamp) / 1000); // 计算订单过期剩余秒数
      // var JustMin = parseInt(sec / 60); //分
      // var JustSec = sec % 60; //秒
      this.setData({
        time:parseInt((newStamp - nowStamp))
      })
    }
  },
  toEticket() {
    if (this.data.ticketOrder.status == 2 || this.data.ticketOrder.status == 5) {
      wx.navigateTo({
        url: '/pages/ScenicArea/ScenicAreaETicket/index?orderNo=' + this.data.ticketOrder.orderNo,
      })
    } else {
      common.showToast('当前订单不可查看电子票')
    }
  },
  toStore() {
    const type = this.data.storeInfo.storeType == 6 ? 1 : 2
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaMainPage/index?moduleType=' + type + '&id=' + this.data.storeInfo.id,
    })
  },
  makePhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.storeInfo.mobile,
    })
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    })
  },
  refund() {
    const that = this
    common.showAlertView('提示', '您确定要申请退款吗？', function (sure) {
      if (sure) {
        const param = {
          orderNo: that.data.ticketOrder.orderNo,
          userId: that.data.userInfo.id,
          token: that.data.token,
        }
        common.post('/ticketOrder/refundTicketOrder', param, function (res) {
          if (res.status === 200) {
            const pages = getCurrentPages()
            const prePage = pages[pages.length - 2]
            wx.navigateBack({
              delta: 1,
              success: function () {
                common.showToast('退款成功')
                if (prePage && prePage.reloadData) {
                  prePage.reloadData()
                }
              }
            })
          } else {
            common.showRequestErrMsg(res)
          }
        })
      }
    })
  }
})